<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.orderNo"
        placeholder="订单号"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="listQuery.userName"
        placeholder="会员名称"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-select v-model="listQuery.score" placeholder="评分" clearable class="filter-item" style="width: 130px">
        <el-option v-for="i in 5" :key="i" :label="`${i}星`" :value="i" />
      </el-select>
      <el-date-picker
        v-model="listQuery.dateRange"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        class="filter-item"
      />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="加载中"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="订单号" width="180">
        <template slot-scope="scope">
          {{ scope.row.orderNo }}
        </template>
      </el-table-column>
      <el-table-column label="会员ID" width="180" prop="userId">
      </el-table-column>
      <el-table-column label="会员姓名" width="180" prop="userName">
      </el-table-column>
      <el-table-column label="评分" width="120" align="center">
        <template slot-scope="scope">
          <el-rate
            v-model="scope.row.score"
            disabled
            show-score
            text-color="#ff9900"
          />
        </template>
      </el-table-column>
      <el-table-column label="评价内容">
        <template slot-scope="scope">
          {{ scope.row.content }}
        </template>
      </el-table-column>
      <el-table-column label="评价时间" width="180" align="center">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="120">
        <template slot-scope="{row}">
          <el-button type="danger" size="mini" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'

export default {
  name: 'ReviewList',
  components: { Pagination },
  data() {
    return {
      list: [
        {
          orderNo: 'OR202401010001',
          userId: '10001',
          userName: '张三',
          score: 5,
          content: '车况很好，服务也很周到，下次还会选择这里租车',
          createTime: '2024-01-03 15:30:00'
        },
        {
          orderNo: 'OR202401010002',
          userId: '10002',
          userName: '李四',
          score: 4,
          content: '整体体验不错，就是取车时间稍微等了一会',
          createTime: '2024-01-05 16:45:00'
        }
      ],
      total: 2,
      listLoading: false,
      listQuery: {
        page: 1,
        limit: 20,
        orderNo: undefined,
        userName: undefined,
        score: undefined,
        dateRange: []
      }
    }
  },
  methods: {
    getList() {
      this.listLoading = true
      setTimeout(() => {
        // 模拟接口调用
        let filteredList = [...this.list]
        if (this.listQuery.orderNo) {
          filteredList = filteredList.filter(item => item.orderNo.includes(this.listQuery.orderNo))
        }
        if (this.listQuery.userName) {
          filteredList = filteredList.filter(item => item.userName.includes(this.listQuery.userName))
        }
        if (this.listQuery.score) {
          filteredList = filteredList.filter(item => item.score === this.listQuery.score)
        }
        this.list = filteredList
        this.total = filteredList.length
        this.listLoading = false
      }, 500)
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    async handleDelete(row) {
      try {
        await this.$confirm('确认删除该评价吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        this.$message.success('删除成功')
        this.getList()
      } catch (error) {
        if (error !== 'cancel') {
          console.error('删除失败:', error)
        }
      }
    }
  }
}
</script>

<style scoped>
.filter-container {
  padding-bottom: 10px;
}
.filter-item {
  margin-right: 10px;
}
</style>